package ru.housewife.service.eatingmenus;

import org.hibernate.Query;
import org.hibernate.Session;
import ru.common.hibernate.HibernateAction;
import ru.housewife.form.EatingMenuForm;

/**
 * removes eating menu
 *
 * @author Alexander Troshanin
 */
public class RemoveEatingMenu implements HibernateAction<Boolean> {

    private EatingMenuForm eatingMenuForm;  // to be removed

    public RemoveEatingMenu(EatingMenuForm eatingMenuForm) {
        this.eatingMenuForm = eatingMenuForm;
    }

    @Override
    public Boolean execute(Session session) {
        Query q = session.createQuery("delete from EatingMenu where id=:eatingMenuId and dayMenu.id=:dayMenuId");
        q.setLong("eatingMenuId", eatingMenuForm.getId());
        q.setLong("dayMenuId", eatingMenuForm.getDayMenu().getId());
        if (q.executeUpdate() == 0) {
            return false;
        }

        eatingMenuForm.getDayMenu().removeEatingMenu(eatingMenuForm.getId());
        return true;
    }
    
}
